package edu.hrbeu.crc.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import edu.hrbeu.crc.common.helper.ListItemHelper;
import edu.hrbeu.crc.constant.StatusConstant;
import edu.hrbeu.crc.context.BaseContext;
import edu.hrbeu.crc.mapper.ShoppingItemMapper;
import edu.hrbeu.crc.mapper.ShoppingListMapper;
import edu.hrbeu.crc.pojo.dto.ShoppingListDTO;
import edu.hrbeu.crc.pojo.entity.ShoppingItem;
import edu.hrbeu.crc.pojo.entity.ShoppingList;
import edu.hrbeu.crc.pojo.query.ShoppingListQuery;
import edu.hrbeu.crc.pojo.result.PageResult;
import edu.hrbeu.crc.pojo.vo.ShoppingItemVO;
import edu.hrbeu.crc.pojo.vo.ShoppingListPageVO;
import edu.hrbeu.crc.pojo.vo.ShoppingListVO;
import edu.hrbeu.crc.service.ShoppingItemService;
import edu.hrbeu.crc.service.ShoppingListService;
import jakarta.annotation.Resource;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDateTime;
import java.util.List;

@Service
public class ShoppingListServiceImpl extends ServiceImpl<ShoppingListMapper, ShoppingList> implements ShoppingListService {

    @Resource
    private ListItemHelper listItemHelper;

    /**
     * 清单创建
     * @param shoppingListDTO
     */
    @Override
    public void add(ShoppingListDTO shoppingListDTO) {
        ShoppingList shoppingList = new ShoppingList();
        BeanUtils.copyProperties(shoppingListDTO, shoppingList);
        // 用户id、初始状态、创建时间
        shoppingList.setUserId(BaseContext.getCurrentId());
        shoppingList.setCreateTime(LocalDateTime.now());

        save(shoppingList);
    }

    /**
     * 分页查询
     * @param shoppingListQuery
     * @return
     */
    @Override
    public PageResult<ShoppingListPageVO> queryShoppingList(ShoppingListQuery shoppingListQuery) {

        Page<ShoppingList> page = shoppingListQuery.toDefaultSortByCreateTimeDesc();

        // 1.设置wrapper
        QueryWrapper<ShoppingList> wrapper = new QueryWrapper<>();
        wrapper.lambda()
                .eq(ShoppingList::getUserId, BaseContext.getCurrentId())
                .like(shoppingListQuery.getName() != null, ShoppingList::getName, shoppingListQuery.getName())
                .eq(shoppingListQuery.getStatus() != null, ShoppingList::getStatus, shoppingListQuery.getStatus());

        // 2.得到结果
        Page<ShoppingList> shoppingListPage = page(page, wrapper);
        List<ShoppingList> records = shoppingListPage.getRecords();
        List<ShoppingListPageVO> list = BeanUtil.copyToList(records, ShoppingListPageVO.class);
        return PageResult.<ShoppingListPageVO>builder()
                .page(shoppingListPage.getPages())
                .total(shoppingListPage.getTotal())
                .list(list)
                .build();
    }

    /**
     * 根据id查询清单
     * @param id
     * @return
     */
    @Override
    public ShoppingListVO get(Integer id) {
        // 查询清单表信息
        ShoppingList shoppingList = getById(id);
        ShoppingListVO vo = new ShoppingListVO();
        BeanUtils.copyProperties(shoppingList, vo);
        // 查询清单内商品表信息
        List<ShoppingItemVO> items = listItemHelper.getByListId(id);
        vo.setItems(items);
        return vo;
    }

    /**
     * 修改清单
     * @param shoppingListDTO
     */
    @Override
    public void updateShoppingList(ShoppingListDTO shoppingListDTO) {
        UpdateWrapper<ShoppingList> wrapper = new UpdateWrapper<>();
        wrapper.lambda()
                .set(shoppingListDTO.getName() != null, ShoppingList::getName,shoppingListDTO.getName())
                .set(shoppingListDTO.getImg() != null, ShoppingList::getImg,shoppingListDTO.getImg())
                .eq(ShoppingList::getId, shoppingListDTO.getId());
        update(wrapper);
    }

    /**
     * 删除清单
     * @param ids
     */
    @Transactional
    @Override
    public void delete(List<Integer> ids) {
        // 1.删除清单中的商品数据
        for (Integer id : ids) {
            listItemHelper.removeByListId(id);
        }
        // 2.删除清单
        this.removeByIds(ids);
    }

    /**
     * 更改清单状态
     * @param id
     */
    @Transactional
    @Override
    public void upStatus(Integer id) {
        // 1.更改清单内所有商品为已购买状态
        listItemHelper.updateItemStatus(id);

        // 2.更改清单状态
        UpdateWrapper<ShoppingList> shoppingListUpdateWrapper = new UpdateWrapper<>();
        shoppingListUpdateWrapper.lambda()
                .set(ShoppingList::getStatus, StatusConstant.ENABLE)
                .eq(ShoppingList::getId, id);
        this.update(shoppingListUpdateWrapper);
    }
}
